//
//  RankListcell.m
//  yigou
//
//  Created by SunChao on 17/2/20.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "RankListcell.h"

@implementation RankListcell

-(void)setFrame:(CGRect)frame
{
    if ([self.type isEqualToString:@"00"]) {
        // 更改x、宽度
        CGFloat kTableTopBorderWidth = 10;
        CGFloat kTableViewCellMargin = 20;
        //    frame.origin.x = kTableBorderWidth;
        //    frame.size.width -= kTableBorderWidth * 2;
        
        // 更改顶部间距、每个cell之间的间距
        frame.origin.y += kTableTopBorderWidth;
        frame.size.height -= kTableViewCellMargin;
        [super setFrame:frame];
    }else{
        [super setFrame:frame];
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.qiuzhangLabel.text = @"部落酋长";
    self.qiuzhangLabel.textColor = [UIColor whiteColor];
    self.qiuzhangLabel.backgroundColor = QINGColor;
    self.qiuzhangLabel.layer.cornerRadius = 10;
    self.qiuzhangLabel.layer.masksToBounds = YES;
    
    
    [self.guanzhuBtn setBackgroundColor:QINGColor];
    self.guanzhuBtn.layer.cornerRadius = 5;
    self.guanzhuBtn.layer.masksToBounds = YES;
    [self.guanzhuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.guanzhuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.guanzhuBtn addTarget:self action:@selector(GuanZhuAction) forControlEvents:UIControlEventTouchUpInside];
    [self.guanzhuBtn setTitle:@"+关注" forState:UIControlStateNormal];
    [self.guanzhuBtn setTitle:@"已关注" forState:UIControlStateSelected];
    
}

- (void)GuanZhuAction{
//    if (self.guanzhuBtn.selected == YES) {
//        [self loadNetworkQuXiaoGuanZhuData];
//    }else{
//        [self loadNetworkGuanZhuData];
//    }
    
    if (_isManger == YES&&[self.type isEqualToString:@"00"]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeGuanLiButton" object:nil];
        return;
    }else if (_isManger == YES){
        if ([self.type isEqualToString:@"01"]) {
            if (self.jinYanOrGuanZhuType ==1) {
                if (self.guanzhuBtn.selected == YES) {
                    [self loadNetworkQuXiaoJinYanData];
                }else{
                    [self loadNetworkJinYanData];
                }
            }else if (self.jinYanOrGuanZhuType ==2){
                if (self.guanzhuBtn.selected == YES) {
                    [self loadNetworkQuXiaoGuanZhuData];
                }else{
                    [self loadNetworkGuanZhuData];
                }
            }
        }
    
    }else if (_isManger == NO){
        if (self.guanzhuBtn.selected == YES) {
            [self loadNetworkQuXiaoGuanZhuData];
        }else{
            [self loadNetworkGuanZhuData];
        }
    }

}

- (void)loadNetworkJinYanData{
    
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *tribe_id = [self.contentDic objectForKey:@"tribe_id"];
    NSString *shield_user_id = [self.contentDic objectForKey:@"user_id"];
    NSString *url = [NSString stringWithFormat:@"%@/hytribe/shieldUsers?tribe_id=%@&shield_user_id=%@", APIYIGOU,tribe_id,shield_user_id];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"------%@",request);
        self.guanzhuBtn.selected = YES;
        [self.guanzhuBtn setBackgroundColor:[UIColor grayColor]];
        [self.guanzhuBtn setTitle:@"已禁言" forState:UIControlStateSelected];
        [ViewFactory showSuccess:@"禁言用户成功"];
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        
    }];
    
}
- (void)loadNetworkQuXiaoJinYanData{
    
//    POST /hytribe/unShieldUsers
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *tribe_id = [self.contentDic objectForKey:@"tribe_id"];
    NSString *shield_user_id = [self.contentDic objectForKey:@"user_id"];
    NSString *url = [NSString stringWithFormat:@"%@/hytribe/unShieldUsers?tribe_id=%@&shield_user_id=%@", APIYIGOU,tribe_id,shield_user_id];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"------%@",request);
        self.guanzhuBtn.selected = NO;
        [self.guanzhuBtn setBackgroundColor:QINGColor];
        [self.guanzhuBtn setTitle:@"禁言" forState:UIControlStateNormal];
        [ViewFactory showSuccess:@"取消禁言用户成功"];
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        
    }];
    
//    POST /hytribe/isShieldUsers判断用户是否被屏蔽
}

- (void)loadNetworkQuXiaoGuanZhuData{

    DYBaseRequest *request = [DYBaseRequest new];
    NSString *follow_obj_id = [self.contentDic  objectForKey:@"user_id"];
    NSString *url = [NSString stringWithFormat:@"%@/userCenter/qxAttountByUserId?follow_obj_type=00&follow_obj_id=%ld", APIYIGOU,[follow_obj_id integerValue]];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"------%@",request);
        [self.guanzhuBtn setBackgroundColor:QINGColor];
        self.guanzhuBtn.selected = NO;
        [ViewFactory showSuccess:@"取消关注成功"];
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];

    }];

}
- (void)loadNetworkGuanZhuData {

    DYBaseRequest *request = [DYBaseRequest new];
    NSString *follow_obj_id = [self.contentDic  objectForKey:@"user_id"];
    NSString *url = [NSString stringWithFormat:@"%@/hytribe/setConcern?follow_obj_id=%ld", APIYIGOU, [follow_obj_id integerValue]];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"------%@",request);
        [self.guanzhuBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        self.guanzhuBtn.selected = YES;
        [ViewFactory showSuccess:@"关注成功"];
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];

    }];
}

-(void)contentWithDic:(NSDictionary *)contentDic type:(NSString *)type;
{
    self.type = type;
    self.contentDic = [NSMutableDictionary dictionaryWithDictionary:contentDic];
    self.nameLabel.text =[contentDic objectForKey:@"nick_name"];
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:[contentDic objectForKey:@"head_url"]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    self.imgView.layer.cornerRadius = 30;
    self.imgView.layer.masksToBounds = YES;
    if (_isManger == YES) {
        if ([[contentDic objectForKey:@"ismyself"] boolValue]==YES) {
            self.guanzhuBtn.hidden = YES;
        }else if ([[contentDic objectForKey:@"ismyself"] boolValue] == NO){
            self.guanzhuBtn.hidden = NO;
        }
        if ([type isEqualToString:@"00"]) {
            self.guanzhuBtn.hidden = NO;
            self.qiuzhangLabel.hidden =  NO;
            self.descLabel.text = [contentDic objectForKey:@"user_signature"];
            self.fensiCountLabel.hidden = YES;
            self.huoyueduLabel.hidden = YES;
            self.huoyueduCountLabel.hidden = YES;
            [self.guanzhuBtn setTitle:@"管理" forState:UIControlStateNormal];
        }
        if ([type isEqualToString:@"01"]) {
            
            if (self.jinYanOrGuanZhuType == 1) {
                self.qiuzhangLabel.hidden = YES;
                self.descLabel.text = @"粉丝数:";
                self.fensiCountLabel.text = [[contentDic objectForKey:@"personcount"] stringValue];
                self.huoyueduLabel.text = @"活跃度";
                self.huoyueduCountLabel.text= [[contentDic objectForKey:@"actionper"] stringValue];
                [self.guanzhuBtn setTitle:@"禁言" forState:UIControlStateNormal];
                [self.guanzhuBtn setTitle:@"已禁言" forState:UIControlStateSelected];
                if ([[self.contentDic objectForKey:@"shield"]integerValue]==YES) {
                    self.guanzhuBtn.selected = YES;
                    [self.guanzhuBtn setBackgroundColor:[UIColor grayColor]];
                }else{
                    self.guanzhuBtn.selected = NO;
                    [self.guanzhuBtn setBackgroundColor:QINGColor];
                }
            }else if (self.jinYanOrGuanZhuType == 2){
        
            self.qiuzhangLabel.hidden = YES;
            self.descLabel.text = @"粉丝数:";
            self.fensiCountLabel.text = [[contentDic objectForKey:@"personcount"] stringValue];
            self.huoyueduLabel.text = @"活跃度";
            self.huoyueduCountLabel.text= [[contentDic objectForKey:@"actionper"] stringValue];
                [self.guanzhuBtn setTitle:@"关注" forState:UIControlStateNormal];
                [self.guanzhuBtn setTitle:@"已关注" forState:UIControlStateSelected];
                if ([[contentDic objectForKey:@"isgz"]intValue] == YES) {
                    self.guanzhuBtn.selected = YES;
                    [self.guanzhuBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
                }else{
                    self.guanzhuBtn.selected = NO;
                    [self.guanzhuBtn setBackgroundColor:QINGColor];
                }
            }
        }
        
        
    }else{
        
        if ([[contentDic objectForKey:@"ismyself"] boolValue]==YES) {
            self.guanzhuBtn.hidden = YES;
        }else if ([[contentDic objectForKey:@"ismyself"] boolValue] == NO){
            self.guanzhuBtn.hidden = NO;
        }
        if ([type isEqualToString:@"00"]) {
            self.qiuzhangLabel.hidden =  NO;
            self.descLabel.text = [contentDic objectForKey:@"user_signature"];
            self.fensiCountLabel.hidden = YES;
            self.huoyueduLabel.hidden = YES;
            self.huoyueduCountLabel.hidden = YES;
        }
        if ([type isEqualToString:@"01"]) {
            self.qiuzhangLabel.hidden = YES;
            self.descLabel.text = @"粉丝数:";
            self.fensiCountLabel.text = [[contentDic objectForKey:@"personcount"] stringValue];
            self.huoyueduLabel.text = @"活跃度";
            self.huoyueduCountLabel.text= [[contentDic objectForKey:@"actionper"] stringValue];
        }
        if ([[contentDic objectForKey:@"isgz"]intValue] == YES) {
            self.guanzhuBtn.selected = YES;
            [self.guanzhuBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        }else{
            self.guanzhuBtn.selected = NO;
            
        }
    }
    
}
@end
